//
//  BanBu_PhotoShowCell.m
// Halfarea
//
//  Created by Wang Xj on 14-3-26.
//
//

#import "BanBu_PhotoShowCell.h"

@implementation BanBu_PhotoShowCell

- (void)dealloc
{
    [_ImgArr release];
    _ImgArr = nil;
    self.photoPressedCallBack = nil;
    [super dealloc];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.contentView.backgroundColor = UIColorWithRGB(237, 239, 241, 1.0);
        _ImgArr = [[NSMutableArray alloc]initWithCapacity:5];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        for (int i = 0; i < 4; i ++)
        {
            UIImageView * photoImgV = [[UIImageView alloc]initWithFrame:CGRectMake(79 * i + 5, 4, 73, 73)];
            photoImgV.tag = 100 + i;
            UIImageView * videoImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"CTAssetsPickerVideo.png"]];
            videoImage.tag = 110;
            [videoImage setHidden:YES];
            [videoImage setFrame:CGRectMake(2, 63, 15, 8)];
            [photoImgV addSubview:videoImage];
            [videoImage release];
            [videoImage setHidden:YES];

            UILabel * coverLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 58, 73, 15)];
            coverLabel.text = NSLocalizedString(@"album_cover", 已设为封面);
            coverLabel.alpha = 0.8;
            coverLabel.backgroundColor = UIColorWithRGB(255, 255, 255, 0.7);
            coverLabel.font = [UIFont systemFontOfSize:10.0];
            coverLabel.textAlignment = kTextAlignmentCenter;
            [photoImgV addSubview:coverLabel];
            [coverLabel release];
            coverLabel.tag = 105;
            UIImageView * shadeImgV  = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 76, 76)];
            shadeImgV.image = [UIImage imageNamed:@"overlay.png"];
            shadeImgV.tag = 200;
            [photoImgV addSubview:shadeImgV];
            [shadeImgV release];
            [shadeImgV setHidden:YES];
            photoImgV.userInteractionEnabled = YES;
            UITapGestureRecognizer * singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(photoImgVTaped:)];
            [photoImgV addGestureRecognizer:singleTap];
            [singleTap release];
            [self.contentView addSubview:photoImgV];
            [photoImgV release];
        }
    }
    return self;
}
/**
 * @函数名称：updatePhotoShowCell:withSelectedArr:withCoverPath:andEdit:
 * @函数描述：(NSArray *)imageArr:要显示的图片信息 (NSArray *)selectedArr：当前选中的图片信息，(BOOL)edit：是否处于编辑状态 (NSString *)coverIds：封面ids
 * @输入参数：void
 * @输出参数：void
 * @返回值：void
 * @author：yinlinlin
 */
- (void)updatePhotoShowCell:(NSArray *)imageArr withSelectedArr:(NSArray *)selectedArr withCoverPath:(NSString *)coverIds andEdit:(BOOL)edit
{
    [_ImgArr removeAllObjects];
    [_ImgArr addObjectsFromArray:imageArr];
    for (int i = 0; i < 4; i ++)
    {
        UIImageView * imageV = (UIImageView *)[self.contentView viewWithTag:100  + i];
        UIImageView * shadeImgV = (UIImageView *)[imageV viewWithTag:200];
        [shadeImgV setHidden:YES];
        [imageV setHidden:YES];
    }
    for (int i = 0; i < imageArr.count; i ++)
    {
        UIImageView * imageV = (UIImageView *)[self.contentView viewWithTag:100 + i];
        [imageV setHidden:NO];
        BanBu_MediaModel * mediaModel = [imageArr objectAtIndex:i];
        UIImageView * videoImgV = (UIImageView *)[imageV viewWithTag:110];
    
        if ([mediaModel.media_type isEqualToString:@"v"])
        {
            [videoImgV setHidden:NO];
        }
        else
        {
            [videoImgV setHidden:YES];
        }
        //如果处于编辑状态，选中图片显示shadeImgV
        if (edit)
        {
            for (BanBu_MediaModel * selectModel in selectedArr)
            {
                if ([selectModel.photo_id isEqualToString:mediaModel.photo_id])
                {
                    UIImageView * shadeImgV = (UIImageView *)[imageV viewWithTag:200];
                    [shadeImgV setHidden:NO];
                    break;
                }
            }
        }
        
        [imageV setImageWithURL:[NSURL URLWithString:mediaModel.thumb_url]];
        
        //如果是封面照片，显示
        UILabel * coverLabel = (UILabel *)[imageV viewWithTag:105];
        if (mediaModel.photo_id.length)
        {
            NSRange range = [coverIds rangeOfString:mediaModel.photo_id];
            NSLog(@"%d %d",range.location,range.length);
            if (range.length == mediaModel.photo_id.length)
            {
                coverLabel.text = NSLocalizedString(@"album_cover", 已设为封面);
                [coverLabel setHidden:NO];
            }
            //        //没有经过审核
            //        else if (!mediaModel.audit)
            //        {
            //            coverLabel.text = NSLocalizedString(@"album_auditing", 审核中);
            //            [coverLabel setHidden:NO];
            //        }
            else
            {
                [coverLabel setHidden:YES];
            }
        }
        else
        {
            [coverLabel setHidden:YES];
        }
    }
    
}
- (void)photoImgVTaped:(UITapGestureRecognizer *)singleTap
{
    NSLog(@"点击了第%d个照片",singleTap.view.tag - 100);
    if (_photoPressedCallBack)
    {
        _photoPressedCallBack(singleTap.view.tag - 100,_ImgArr);
    }
    
}
- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
